/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include "cgpanel/raytracer/bvhtree.h"
#include "cgpanel/raytracer/bvhnode.h"
#include "objects/plane.h"

namespace draw
{
BVHTree::BVHTree()
{
  root = NULL;
}

void BVHTree::sortToCentersToX(QList<object::Plane*> *A, int lengte)
{
  int i;
  int j;
 
  if( lengte <= 1 ) return;
  
  for(i = 1 ; i < lengte; ++i){
    for( j=1; A->at((i-j+1)/j) > 0 && A->at( ( i-j+1)/j)->getCenterY() >
A->at( (i-2*j+1)/(2*j) )->getCenterY(); j*=2)
    {
      A->swap( (i-j+1)/j, (i-2*j+1)/(2*j) );
    }
  }
  
   A->swap(0,lengte-1);
   sortToCentersToX(A,lengte-1);
   return;
}

BVHTree::~BVHTree()
{
  if(root != NULL)
    delete root;
}

void BVHTree::initialize(QVector<object::Plane*> A)
{
  QList<object::Plane*> AtoList = A.toList();
  //sortToCentersToX(&AtoList, A.size());
  root = new BVHNode(&AtoList, 0);
}
  
};

